package com.shiny.concurrency4java.lock;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DeadLockExample {

    private static String A = "A";
    private static String B = "B";

    public static void main(String[] args) {
        new DeadLockExample().deadLock();
    }

    private void deadLock(){
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (A){
                    try{
                        Thread.currentThread().sleep(1000);
                        log.info("deadLock function start synchronized A {}",Thread.currentThread().getName());
                        synchronized (B) {
                            log.info("deadLock function start synchronized B",Thread.currentThread().getName());
                        }

                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"Thread1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (B) {
                    try{
                        Thread.currentThread().sleep(1000);
                        log.info("deadLock function start synchronized B {}",Thread.currentThread().getName());
                        synchronized (A) {
                            log.info("deadLock function start synchronized A",Thread.currentThread().getName());
                        }

                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"Thread2");
        t1.start();
        t2.start();
    }

}
